package org.cerberus.platform.ograph;

import java.lang.reflect.Method;

public class ComponentClassAttribute {

	private String name;
	private Class<?> type;
	private String classID;
	
	private Method setter;
	private Method getter;
	
	private int index;
	private int resolvedIndex;
	
	ComponentClassAttribute(String name, 
			Class<?> type,
			String classID,
			Method getter,
			Method setter) {
		this.name = name;
		this.classID = classID;
		this.type = type;
		this.setter = setter;
		this.getter = getter;
	}

	void defineIndex(int localIndex, int resolvedIndex) {
		this.index = localIndex;
		this.resolvedIndex = resolvedIndex;
	}
	
	public boolean isReadOnly() {
		return setter == null || isComponent();
	}
	
	public boolean isNative() {
		return NativeClasses.isNativeClassID(classID);
	}
	
	public boolean isComponent() {
		return !isNative();
	}
	
	public String getName() {
		return name;
	}

	public Class<? extends Object> getType() {
		return type;
	}

	public Method getSetter() {
		return setter;
	}

	public Method getGetter() {
		return getter;
	}

	public Object getValue(Object source) throws Throwable {
		return getter.invoke(source);
	}
	
	public void setValue(Object source, Object value) throws Throwable {
		if (!isReadOnly()) {
			setter.invoke(source, value);
		} else {
			throw new ComponentClassException("Attribute "+toString()+" is not editable.");
		}
	}
	
	public int getIndex() {
		return index;
	}

	public int getResolvedIndex() {
		return resolvedIndex;
	}

	public String getClassID() {
		return classID;
	}
	
}
